package com.experienceBaby.Cashbook;

import android.content.Context;
import android.database.Cursor;
import android.util.Log;
import android.widget.TextView;

import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.ext.junit.runners.AndroidJUnit4;

import com.experienceBaby.Cashbook.bean.Daily;
import com.experienceBaby.Cashbook.bean.Ranking;
import com.experienceBaby.Cashbook.bean.RankingList;
import com.experienceBaby.Cashbook.greendao.DbController;
import com.experienceBaby.Cashbook.greendao.db.Recode;
import com.experienceBaby.Cashbook.utils.DateUtils;

import org.junit.Test;
import org.junit.runner.RunWith;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static org.junit.Assert.*;

/**
 * Instrumented test, which will execute on an Android device.
 *
 * @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
 */
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
    Context appContext;
    DbController mDbController;

    String strDateFormat = "yyyy-MM-dd";
    SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);

    @Test
    public void useAppContext() {
        // Context of the app under test.
        appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
        assertEquals("com.experienceBaby.Cashbook", appContext.getPackageName());


        mDbController = DbController.getInstance(appContext);
        Date date = new Date();
        long time = date.getTime();


        Date date1 = DateUtils.getDate(2021, 7, 31);
        long time1 = date1.getTime();

        Recode recode1 = new Recode(null, "测试", "生活", time1, sdf.format(date1), (Math.random() * 200), 0);
        Recode recode2 = new Recode(null, "测试2", "旅游", time1, sdf.format(date1), (Math.random() * 200), 0);
        Recode recode3 = new Recode(null, "测试3", "生活", time1, sdf.format(date1), (Math.random() * 200), 1);

//        for (int i = 1; i < 31; i++) {
//            Date datee = DateUtils.getDate(2021, 6, i);
//            long timee = datee.getTime();
//            Recode recode = new Recode(null, "测试", "生活", timee, sdf.format(datee), (Math.random() * 200), 0);
//            mDbController.insertOrReplace(recode);
//
//        }

//        mDbController.insertOrReplace(recode1);
//        mDbController.insertOrReplace(recode2);
//        mDbController.insertOrReplace(recode3);

//        Test();
//        Test2();

//        newTest();
//        queryTest(DateUtils.getThisMonth(date1.getTime()), DateUtils.getDayOfMonth(date1.getTime()));
//        mDbController.searchAll();
        test();
    }

    private void test() {


        List<Double> costs = mDbController.searchByMonthGroupByType(2021, 6, false).getCosts();
        for (int i = 0; i < costs.size(); i++) {
            System.out.println();
        }
        System.out.println();
    }

    private void newTest() {
        Ranking ranking = mDbController.searchByMonthGroupByType(2021, 2, true);
        System.out.println();

    }

    private void queryTest(Long[] time, int period) {
        Cursor c = mDbController.getReadableDatabase().rawQuery("SELECT TYPE,sum(COST) AS s FROM RECODE where DATE between " + time[0] + " AND " + time[1] + " GROUP BY TYPE ORDER BY s DESC;", null);
        //每一行都是一个种类
        Ranking ranking = new Ranking();
        double totalCost = 0;
        List<RankingList> rankingLists = new ArrayList<>();
        while (c.moveToNext()) {
            RankingList rankingList = new RankingList();
            rankingList.setCost(c.getDouble(1));
            totalCost += c.getDouble(1);
            rankingList.setType(c.getString(0));
            rankingLists.add(rankingList);
        }
        for (RankingList rankingList : rankingLists) {
            rankingList.setPercentage(rankingList.getCost() / totalCost);
        }
        ranking.setDatas(rankingLists);
        ranking.setLotalCost(totalCost);
        ranking.setAvgCost(totalCost / period);
        //TODO 后面用框架挨个查询
//        //每天的消费
        Cursor c1 = mDbController.getReadableDatabase().rawQuery("select DATE,sum(COST) as s FROM RECODE where DATE between " + time[0] + " AND " + time[1] + " group by S_DATE order by DATE asc", null);
//        while (c1.moveToNext()) {
//
//        }
        System.out.println();
    }

    public void Test() {
        List<Daily> dailies = mDbController.searchByMonth(2021, 2);

        for (int i = 0; i < dailies.size(); i++) {
        }
    }

    public void Test2() {
        Long[] l = DateUtils.getThisYear(DateUtils.getDate(new Date().getTime()).getTime());
        Date date = new Date(l[0]);
        Date date1 = new Date(l[1]);


        System.out.println();
    }
}